﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class P01_Add
    Inherits System.Web.UI.Page

    Public scripttext As String
    Dim lockhash As New Hashtable

    Private Shared logger As ILog = LogManager.GetLogger("P01_Add")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            scripttext = "ClosePop();"
        End If

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(Session("PlanYear")) & " "
        rsql += " AND Version = '" & Session("Versions").ToString & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "


        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next

    End Sub

    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim ds As New DataSet
        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If fctr <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & fctr & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            'Response.Write(sql & "<br />")
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
        Else
            'ds = PublicFunction.GetChild2(Session("users"))
            'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
            'e.DataSource = ds
            Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
            Dim dt As New BasicData.FCTR_Child2DataTable
            Dim sql = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
            sql += "ORDER BY [Level] ASC, FundCenterCode ASC"
            adap.GetDataByFilter(dt, sql)
            e.DataSource = dt
            If dt.Count = 1 Then
                FundCenterCombo.Value = dt.Item(0).FundCenterCode
            End If
        End If


        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub ProductCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles ProductCombo.InitializeDataSource
        Dim adap As New SplitBudgetDSTableAdapters.ProductsTableAdapter
        Dim dt As New SplitBudgetDS.ProductsDataTable
        dt = adap.GetDataByP01(Val(Session("PlanYear")))
        e.DataSource = dt
    End Sub

    Protected Sub SaveDocument_Click(sender As Object, e As EventArgs) Handles SaveDocument.Click
        If FundCenterCombo.Value = "" Then
            scripttext = "alert('กรุณาเลือก Fund Center');"
            Exit Sub
        End If
        If ProductCombo.Value = "" Then
            scripttext = "alert('กรุณาเลือกผลิตภัณฑ์');"
            Exit Sub
        End If
        If lockhash(FundCenterCombo.Value.ToString.Trim) = True Then
            scripttext = "alert('ไม่สามารถบันทึกได้เนื่องจากมีการส่งแผนไปแล้ว');"
            Exit Sub
        End If

        Dim adapter1 As New MSinchuaTableAdapters.BudgetRevPlans1TableAdapter
        Dim adapter2 As New PDataSetTableAdapters.ProductRevsTableAdapter
        Dim adapter3 As New PDataSetTableAdapters.BudgetDetailsTableAdapter
        Dim adapter4 As New MSinchuaTableAdapters.ProductItemsTableAdapter
        Dim adapter5 As New PDataSetTableAdapters.ProductItemRevsTableAdapter
        Dim dataX As New MSinchua.BudgetRevPlans1DataTable
        Dim counterX As Decimal = 0
        dataX = adapter1.GetDataByCombos(ProductCombo.Value, FundCenterCombo.Value, Val(Session("PlanYear")), Session("Versions").ToString)
        Dim datax2 As New PDataSet.ProductItemRevsDataTable
        For Each rowx As MSinchua.BudgetRevPlans1Row In dataX
            'datax2 = adapter5.GetDataByFORMS(Request("FormID"), Val(Session("PlanYear")))
            'For Each rowx3 As PDataSet.ProductItemRevsRow In datax2
            '    If rowx.ProductItem_Id = rowx3.ProductItem_Id And rowx3.FormId.ToString = Request("FormID").ToString Then
            counterX = counterX + 1
            '        End If
            'Next
        Next

        If counterX = 0 Then
            Dim actual1_1 As Decimal = 0D
            Dim actual2_1 As Decimal = 0D
            Dim actual3_1 As Decimal = 0D
            Dim actual4_1 As Decimal = 0D
            Dim actual5_1 As Decimal = 0D

            Dim actual1_2 As Decimal = 0D
            Dim actual2_2 As Decimal = 0D
            Dim actual3_2 As Decimal = 0D
            Dim actual4_2 As Decimal = 0D
            Dim actual5_2 As Decimal = 0D

            Dim actual1_5 As Decimal = 0D
            Dim actual2_5 As Decimal = 0D
            Dim actual3_5 As Decimal = 0D
            Dim actual4_5 As Decimal = 0D
            Dim actual5_5 As Decimal = 0D

            Dim Actual1 As Decimal = 0
            Dim Actual2 As Decimal = 0
            Dim Actual3 As Decimal = 0
            Dim Actual4 As Decimal = 0
            Dim Actual5 As Decimal = 0

            Dim rowId1 As Integer
            Dim rowId3 As Integer
            Dim rowId4 As Integer
            Dim rowId6 As Integer

            Dim sum1 As Decimal = 0
            Dim sum2 As Decimal = 0

            Dim sum1_1 As Decimal = 0
            Dim sum1_2 As Decimal = 0
            Dim sum1_5 As Decimal = 0

            Dim cmmtcode As String

            Dim data3 As New MSinchua.ProductItemsDataTable
            data3 = adapter4.GetDataByFormID(Request("FormID"), Val(Session("PlanYear")))
            For Each row2 As MSinchua.ProductItemsRow In data3
                cmmtcode = adapter2.GetCMmtCode(ProductCombo.Value, row2.Id)
                'MsgBox("ProNO = " & row2.ProItemID & "  cmmt = " & cmmtcode & " procode = " & ProductCombo.Value & " proid = " & row2.Id)

                Actual1 = 0
                Actual2 = 0
                Actual3 = 0
                Actual4 = 0
                Actual5 = 0

                '1	รับฝากเพิ่ม
                '2	ครบอายุ/ถอนคืน
                '3	คงเหลือ
                '4	เพิ่มสุทธิ
                '5	ดอกเบี้ยจ่าย
                '6	อัตราดอกเบี้ยเฉลี่ย
                Select Case row2.ProItemID
                    Case 1
                        sum1 = Math.Abs(adapter3.GetSumLast2(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value, 0))
                        Actual1 = Math.Abs(adapter3.GetValue1_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 0))
                        Actual2 = Math.Abs(adapter3.GetValue2_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 0))
                        Actual3 = Math.Abs(adapter3.GetValue3_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 0))
                        Actual4 = Math.Abs(adapter3.GetValue4_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 0))
                        Actual5 = Math.Abs(adapter3.GetValue5_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 0))

                        sum1_1 = sum1
                        actual1_1 = Actual1
                        actual2_1 = Actual2
                        actual3_1 = Actual3
                        actual4_1 = Actual4
                        actual5_1 = Actual5

                        sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
                        adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), row2.Id)

                        rowId1 = row2.Id
                    Case 2
                        sum1 = Math.Abs(adapter3.GetSumLast2(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value, 1))
                        Actual1 = Math.Abs(adapter3.GetValue1_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1))
                        Actual2 = Math.Abs(adapter3.GetValue2_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1))
                        Actual3 = Math.Abs(adapter3.GetValue3_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1))
                        Actual4 = Math.Abs(adapter3.GetValue4_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1))
                        Actual5 = Math.Abs(adapter3.GetValue5_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1))

                        sum1_2 = sum1
                        actual1_2 = Actual1
                        actual2_2 = Actual2
                        actual3_2 = Actual3
                        actual4_2 = Actual4
                        actual5_2 = Actual5

                        sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
                        adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), row2.Id)
                    Case 3
                        rowId3 = row2.Id
                    Case 4
                        rowId4 = row2.Id
                    Case 5
                        'sum1 = adapter3.GetSumLast2(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value, 1)
                        'Actual1 = adapter3.GetValue1_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1)
                        'Actual2 = adapter3.GetValue2_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1)
                        'Actual3 = adapter3.GetValue3_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1)
                        'Actual4 = adapter3.GetValue4_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1)
                        'Actual5 = adapter3.GetValue5_2("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value, 1)

                        sum1 = Math.Abs((adapter3.GetSumLast1(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value)))
                        Actual1 = Math.Abs(adapter3.GetValue1_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value))
                        Actual2 = Math.Abs(adapter3.GetValue2_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value))
                        Actual3 = Math.Abs(adapter3.GetValue3_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value))
                        Actual4 = Math.Abs(adapter3.GetValue4_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value))
                        Actual5 = Math.Abs(adapter3.GetValue5_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value))

                        sum1_5 = sum1
                        actual1_5 = Actual1
                        actual2_5 = Actual2
                        actual3_5 = Actual3
                        actual4_5 = Actual4
                        actual5_5 = Actual5

                        sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
                        adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), row2.Id)
                    Case 6
                        rowId6 = row2.Id
                    Case Else
                        'sum1 = adapter3.GetSumLast1(Val(Session("PlanYear") - 2), cmmtcode, FundCenterCombo.Value)
                        'Actual1 = adapter3.GetValue1_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        'Actual2 = adapter3.GetValue2_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        'Actual3 = adapter3.GetValue3_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        'Actual4 = adapter3.GetValue4_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        'Actual5 = adapter3.GetValue5_1("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)
                        sum1 = 0
                        Actual1 = 0
                        Actual2 = 0
                        Actual3 = 0
                        Actual4 = 0
                        Actual5 = 0
                End Select
            Next

            '1	รับฝากเพิ่ม
            '2	ครบอายุ/ถอนคืน
            '3	คงเหลือ
            '4	เพิ่มสุทธิ
            '5	ดอกเบี้ยจ่าย
            '6	อัตราดอกเบี้ยเฉลี่ย

            'เพิ่มสุทธิ
            sum1 = sum1_1 - sum1_2
            Actual1 = actual1_1 - actual1_2
            Actual2 = actual2_1 - actual2_2
            Actual3 = actual3_1 - actual3_2
            Actual4 = actual4_1 - actual4_2
            Actual5 = actual5_1 - actual5_2
            sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
            adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), rowId4)

            'คงเหลือ 
            ' find last year balance
            cmmtcode = adapter2.GetCMmtCode(ProductCombo.Value, rowId1)
            sum1 = Math.Abs(Val(adapter3.GetBalance("A01", Val(Session("PlanYear") - 1), cmmtcode, FundCenterCombo.Value)))
            '
            Actual1 = sum1 + Actual1 'this shoud be minus, in case of positive it will display as minus value then user shall understand the imported data may incorrect
            Actual2 = Actual1 + (actual1_1 - actual1_2)
            Actual3 = Actual2 + (actual3_1 - actual3_2)
            Actual4 = Actual3 + (actual4_1 - actual4_2)
            Actual5 = Actual4 + (actual5_1 - actual5_2)
            sum2 = Actual1 + Actual2 + Actual3 + Actual4 + Actual5
            adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), rowId3)

            'อัตราดอกเบี้ยจ่าย = ( ดอกเบี้ยจ่าย*12 )/(คงเหลือ/100)
            sum1 = 0

            If ((Actual1 / 100) = 0) Then
                Actual1 = 0
            Else
                Actual1 = (actual1_5 * 12) / (Actual1 / 100)
            End If
            If ((Actual2 / 100) = 0) Then
                Actual2 = 0
            Else
                Actual2 = (actual2_5 * 12) / (Actual2 / 100)
            End If
            If ((Actual3 / 100) = 0) Then
                Actual3 = 0
            Else
                Actual3 = (actual3_5 * 12) / (Actual3 / 100)
            End If
            If ((Actual4 / 100) = 0) Then
                Actual4 = 0
            Else
                Actual4 = (actual4_5 * 12) / (Actual4 / 100)
            End If
            If ((Actual5 / 100) = 0) Then
                Actual5 = 0
            Else
                Actual5 = (actual5_5 * 12) / (Actual5 / 100)
            End If

            sum2 = (Actual1 + Actual2 + Actual3 + Actual4 + Actual5) / 12
            adapter1.InsertTemp(Val(Session("PlanYear")), Val(Session("PlanYear")), Session("Versions"), Val(sum1), Val(sum2), 0, 0, Val(Actual1), Val(Actual2), Val(Actual3), Val(Actual4), Val(Actual5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Date.Now(), Nothing, Session("users"), Nothing, FundCenterCombo.Value, Val(Session("PlanYear")), ProductCombo.Value, Val(Session("PlanYear")), rowId6)
        Else
            scripttext = "alert('ไม่สามารถเพิ่มข้อมูลซ้ำได้');"
            Exit Sub
        End If

        logger.Info("User " & Session("users") & " product code " & ProductCombo.Value & " for fund center " & FundCenterCombo.Value)

        scripttext = "ClosePop();"
    End Sub
End Class
